function semName(semester) {
/*Examples are:
	***2014 academic year***
	2014 semester 3 is Spring 2014
	2014 semester 2 is Fall 2013
	2014 semester 1 is Summer 2013
	***2013 academic year***
	2013 semester 3 is Spring 2013
	2013 semester 2 is Fall 2012
	2013 semester 1 is Summer 2012*/
	switch (semester) {
		case 1:
			return "Summer";
			break;
		case 2:
			return "Fall";
			break;
		case 3:
			return "Spring";
			break;
		default:
			return "Winter";
			year = 1980;
			break;
	}
};


function getCorrectYear(year, semester) {
	switch (semester) {
		case 1:
		case 2:
			return (year - 1);
			break;
		case 3:
			return year;
			break;
		default:
			return 99;
			break;
	}
};



/*["CLASS","BANNERCRN","SECTION","SEMESTER","BLOCK","YEAR","SUBJECTCODE","COURSENUMBER","InstructID","FIRSTNAME","LASTNAME","EMAIL","SCORE","RESPONSES","STUDENTS","STANDARDDEVIATION","TESTID","TESTNAME"]*/
/*
Instructor IDs
Brad Peterson 		887969243
Luke Fernandez 		880519503
Allison Sunderland  550035577
Yong Zhang			550093614
Ted Cowan			880329877
Delroy Brinkerhoff	880644109
Drew Weidman		886655283
Richard Fry			887999808
*/

var InstructID;
var year;
var dept='CS';

window.onload = (function() 
{
	var select = document.getElementById("firstDropDown");

	// HOW TO HANDLE THE ? SPLIT ON FIRST LOAD?
	var href = window.location.href;
	var instructor = -1;
	
	if(href.indexOf('?') != -1){
	
		var query = href.split('?'); 
		var params = query[1].split('=');
		
		
		if(params[1] != null){
		
			instructor = params[1];
		
		}
		
	}
	jQuery.get('https://chitester1dev.weber.edu:6838/misc/weber/CSEvals/ProfessorsWithAnswers.cfm?dept='+dept, function(data) 
	{	//option.innerHTML = 'Select and Instructor';
		//select.appendChild(option);
		for (var i = 0; i < data.DATA.length; i++) 
		{
  			var option = document.createElement("option");
  			var at = data.DATA[i];
  			var teacherid = at[0];
  			var name = at[1] + " " + at[2];
  			option.value = teacherid;
  			option.innerHTML = name;
  			select.appendChild(option);  
  			
  			if(instructor != -1)
				{
  				if (option.value == instructor) 
					{
  					$('#firstDropDown option').prop('selected', true);	
  				
						
					}
				}
		}
		InstructID = document.getElementById("firstDropDown").value;
		//document.getElementById("firstDropDown").onChange = function yearSelect();
	}).success(function() 
	{
		
		getData();
	});
});
	function yearSelect() {
	//InstructID comes up undefined

	var yearsValue = -1;
	
	var href = window.location.href;
	sel = document.getElementById("secondDropDown");

	//http://www.teacheroverall.html/?instructId=847654&year=2013
	if(href.indexOf('&') != -1)
	{
			var query = href.split('?');
			var query2 = query[1].split('&');
			var params = query2[1].split('=');
			//This is supposed to be a year value
			if(params[1] != null)
			{
				yearsValue = params[1];
			}
			/*RESULT OF YearsTaught
			{"COLUMNS":["INSTRUCTORID","SEMESTER","YEAR"],
			"DATA":
			[
			["887969243","1","2011"],
			["887969243","1","2012"],
			["887969243","1","2013"],
			["887969243","2","2011"],
			["887969243","2","2013"],
			["887969243","3","2012"],
			["887969243","3","2013"]
			]
			}
			
		}	*/
		}
		jQuery.get('https://chitester1dev.weber.edu:6838/misc/weber/CSEvals/YearsTaught.cfm?InstructorID='+InstructID, function(YEARS)
			{
					for (var n = 0; n < YEARS.DATA.length; n++) 
					{
						var option = document.createElement("option");
						var at = YEARS.DATA[n]
						var teacherid = at[0];
						var semester = at[1];
						var year = at[2];
						option.value = year;
						option.innerHTML = semName(semester) + " " + year;
						sel.appendChild(option);
						if(yearsValue != -1)
						{
							if (option.value == yearsValue) 
							{
								$('#secondDropDown option').prop('selected', true);	
								
							}
						}
						
					}
					year = document.getElementById("secondDropDown").value;	
			})
};
function submitFilters(){

	window.location.href = "Teacher_Overall.html?instruct=" + $('#firstDropDown').val()+'&year='+$('#secondDropDown').val();
}	

/*
LEFT TO DO:	
Ticks 2-5 png put into place
Year drop working correctly
Link class crn to class breakdown class.
*/


function getData() {	
	$.get('https://chitester1dev.weber.edu:6838/misc/weber/CSEvals/ClassesTaught.cfm?InstructorID='+InstructID+'&year='+year, function(data) {
	

		/*variables for semester and year for the header to the table*/
		var semesterName;
		/*var year;*/
		var currSemester;
		var currYear;		
		var output = "";
		
	
		$.each(data.DATA, function(i,courseInfo) {
			if (!currSemester && !currYear) {
				currSemester = courseInfo[3];
				currYear = courseInfo[5];
				output+=('<table class="teacher_overall_table">');
				output+=('<caption><p class="table_header">'+semName(Number(courseInfo[3]))+' - '+getCorrectYear(Number(courseInfo[5]),Number(courseInfo[3]))+'</p></caption>');
				output+=('<tr><th class="inst_name">Instructor Name</th><th class="class_crn">Class - CRN</th><th class="resp"># Resp.</th><th class="std_dev">STD Dev.</th><th class="score">Score</th><th class="test_name">Test Name</th><th></th></tr>');
			} else if (currYear != courseInfo[5] || currSemester != courseInfo[3]) {
				output+=('</table><br />');
				currSemester = courseInfo[3];
				currYear = courseInfo[5];				
				output+=('<table class="teacher_overall_table">');
				output+=('<caption><p class="table_header">'+semName(Number(courseInfo[3]))+' - '+getCorrectYear(Number(courseInfo[5]),Number(courseInfo[3]))+'</p></caption>');
				output+=('<tr><th class="inst_name">Instructor Name</th><th class="class_crn">Class - CRN</th><th class="resp"># Resp.</th><th class="std_dev">STD Dev.</th><th class="score">Score</th><th class="test_name">Test Name</th><th></th></tr>');
			}
				
			var stdDev = Number(courseInfo[15]);
			var score = Number(courseInfo[12]);
			var scoreEvalCollegeCoastOnline = Number(courseInfo[12]) + 1;
			var scoreBar = score/4*300; 
			
			/*if row is an even row*/
			if (i % 2 == 0) {
				/*If test is 'Eval Dept of CS', background is blue*/
				if (courseInfo[16] == 64529) {
					output+=('<tr class="table_row_even"><td><div class="ellipsis">'+courseInfo[9]+' '+courseInfo[10]+'</div></td><td><a href="">'+courseInfo[0]+' - '+courseInfo[1]+'</a></td><td>'+courseInfo[14]+'</td><td>'+stdDev.toFixed(1)+'</td><td>'+score.toFixed(2)+'</td><td class="test_name"><div class="ellipsis">'+courseInfo[17]+'</div></td><td class="fill_pixels_score"><div class="outer-box ticksZeroToFour"><div class = "container ticksZeroToFour" style="height:20px;width:'+scoreBar+'px;background-color:#1D8BCD;float:left;"></div><div class="sm_container ticksZeroToFour"></div></div></td></tr>');
				/*If test is 'Eval College Coast Online', background is purple*/
				} else if (courseInfo[16] == 35415) {
					output+=('<tr class="table_row_even"><td><div class="ellipsis">'+courseInfo[9]+' '+courseInfo[10]+'</div></td><td><a href="">'+courseInfo[0]+' - '+courseInfo[1]+'</a></td><td>'+courseInfo[14]+'</td><td>'+stdDev.toFixed(1)+'</td><td>'+scoreEvalCollegeCoastOnline.toFixed(2)+'</td><td class="test_name"><div class="ellipsis">'+courseInfo[17]+'</div></td><td class="fill_pixels_score"><div class="outer-box ticksTwoToFive"><div class = "container ticksTwoToFive" style="height:20px;width:'+scoreBar+'px;background-color:#A020F0;float:left;"></div><div class="sm_container ticksTwoToFive"></div></div></td></tr>');
				/*every other test, no background score bar or ticks image*/
				} else {
					output+=('<tr class="table_row_even"><td><div class="ellipsis">'+courseInfo[9]+' '+courseInfo[10]+'</div></td><td><a href="">'+courseInfo[0]+' - '+courseInfo[1]+'</a></td><td>'+courseInfo[14]+'</td><td>'+stdDev.toFixed(1)+'</td><td>'+score.toFixed(2)+'</td><td class="test_name"><div class="ellipsis">'+courseInfo[17]+'</div></td><td class="fill_pixels_score"><div class="noTicks"></div></td></tr>');
				}
			/*if row is an odd row*/
			} else {
				/*If test is 'Eval Dept of CS', background is blue*/
				if (courseInfo[16] == 64529) {
					output+=('<tr class="table_row_odd"><td><div class="ellipsis">'+courseInfo[9]+' '+courseInfo[10]+'</div></td><td><a href="">'+courseInfo[0]+' - '+courseInfo[1]+'</a></td><td>'+courseInfo[14]+'</td><td>'+stdDev.toFixed(1)+'</td><td>'+score.toFixed(2)+'</td><td class="test_name"><div class="ellipsis">'+courseInfo[17]+'</div></td><td class="fill_pixels_score"><div class="outer-box ticksZeroToFour"><div class = "container ticksZeroToFour" style="height:20px;width:'+scoreBar+'px;background-color:#1D8BCD;float:left;"></div><div class="sm_container ticksZeroToFour"></div></div></td></tr>');
				/*If test is 'Eval College Coast Online', background is purple*/
				} else if (courseInfo[16] == 35415) {
					output+=('<tr class="table_row_odd"><td><div class="ellipsis">'+courseInfo[9]+' '+courseInfo[10]+'</div></td><td><a href="">'+courseInfo[0]+' - '+courseInfo[1]+'</a></td><td>'+courseInfo[14]+'</td><td>'+stdDev.toFixed(1)+'</td><td>'+scoreEvalCollegeCoastOnline.toFixed(2)+'</td><td class="test_name"><div class="ellipsis">'+courseInfo[17]+'</div></td><td class="fill_pixels_score"><div class="outer-box ticksTwoToFive"><div class = "container ticksTwoToFive" style="height:20px;width:'+scoreBar+'px;background-color:#A020F0;float:left;"></div><div class="sm_container ticksTwoToFive"></div></div></td></tr>');
				/*every other test, no background score bar or ticks image*/
				} else {
					output+=('<tr class="table_row_odd"><td><div class="ellipsis">'+courseInfo[9]+' '+courseInfo[10]+'</div></td><td><a href="">'+courseInfo[0]+' - '+courseInfo[1]+'</a></td><td>'+courseInfo[14]+'</td><td>'+stdDev.toFixed(1)+'</td><td>'+score.toFixed(2)+'</td><td class="test_name"><div class="ellipsis">'+courseInfo[17]+'</div></td><td class="fill_pixels_score"><div class="noTicks"></div></td></tr>');
				}
			}
		});
			
		output+=('</table>');
		$("#Anchor").append(output);
	
	})
}